/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
//using GA_ScreenScraper;
using ManagedWinapi.Windows;
using ManagedWinapi.Accessibility;
using HtmlAgilityPack;
using LucasCode.Win32;
using System.Management;

namespace LucasCode.Tools
{
    public static class ESLHelpers
    {
        public class ParseException : Exception
        {
            public ParseException(string error)
                : base(error)
            {
                //relax
            }
            //nothing
        };

        public static string GetHTML(string URL, string cook)
        {
            return GetHTML(URL, new Cookie("COOKIE", cook, "/", ".esl.eu"));
        }
        
        public static string GetHTML(string URL, Cookie cook)
        {
            string HTML = String.Empty;
            HttpWebRequest request;
            HttpWebResponse response;
            StreamReader answer = null;
            request = (HttpWebRequest)WebRequest.Create(URL);
            request.Timeout = 10000;
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(cook);
            //get response
            
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                answer = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
                //while (m_UploadStatus != ItemUploadStatus.Aborted && answer.ReadLine() != String.Empty)
                //{
                //HTML += answer.ReadLine();
                //}
                HTML = answer.ReadToEnd();
            }
            catch (Exception ex)
            {
                HTML = String.Empty;
                return HTML;
            }
            finally
            {
                if(answer != null)
                    answer.Close();
            }
            return HTML;
        }

        public static bool isAequitasMonitoring()
        {
            Thread.Sleep(5000);
            SystemWindow[] ToplevelWindows = SystemWindow.AllToplevelWindows;
            foreach (SystemWindow win in ToplevelWindows)
            {
                //Debug.WriteLine(win.Title);
                if (win.Title.StartsWith("ESL Aequitas"))
                {
                    Debug.WriteLine("Untersuche " + win.HWnd.ToString());
                    SystemWindow[] controls = win.AllChildWindows;
                    foreach (SystemWindow control in controls)
                    {
                        //Debug.WriteLine(control.Title);
                        if (control.Title == "End Monitoring")
                            return control.Enabled;
                        //MessageBox.Show(control.Enabled ? "Running!" : "Not running!");
                    }
                }
            }
            return false;
        }
    }
}
